package com.boot.study.unit;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.boot.study.TestExcelDemo;
import com.boot.study.domain.YearResolve;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: lkz
 * @Title: testController
 * @Description: TODO
 * @Date: 2023/7/28 17:22
 */
@RestController
public class TestController {

    @GetMapping("test")
    public void test(HttpServletResponse response) throws IOException {

        ExcelWriter excelWriter=null;
        ServletOutputStream outputStream=null;

        TestExcelDemo testExcelDemo = new TestExcelDemo();
        List<YearResolve> yearResolves = testExcelDemo.initYear();

        try  {

            // 写入list之前的数据
            Map<String, Object> map = new ConcurrentHashMap<>();
            //研究中心总费用
            testExcelDemo.initDataStuCoreSumCost(map);
            //药品总费用
            testExcelDemo.initDataMedicineCostSum(map);
            //供应商总费用
            testExcelDemo.initDataSupSumCost(map);
            // 其他总费用
            testExcelDemo.initDataOtherSumCost(map);
            //文章发表总费用
            testExcelDemo.initDataArticle(map);
            //Ⅵ-赠药成本汇总
            //Ⅶ-人力成本总费用

            map.put("projectName", "项目名称");
            map.put("versionTime", "2019年10月9日13:28:28");
            map.put("schemeCode", "方案版本号");
            map.put("createReportTime", DateUtil.format(new Date(),"yyyy-MM-dd"));
            map.put("actCostSum", 10);
            map.put("presentMedicCostSum", 20);
            map.put("manCostSum", 20000);
            map.put("preManCostSum", 1000);
            map.put("proCostSum", 562);
            map.put("coreNum", 4343);
            map.put("subNum", 434);
            map.put("proBegTime", "2023-07-21"); // 项目开始时间
            map.put("proEndTime", "2023-07-21");// 项目结束时间
            map.put("typeStuCoreSumCost", map.get("typeStuCoreSumCost")); //研究中心总费用
            map.put("typeMedicineSumCost", map.get("typeMedicineSumCost"));//药品总费用
            map.put("typeSupSumCost", map.get("typeSupSumCost"));//供应商总费用
            map.put("manySystemCostSumAdd", (String)map.get("manySysCostSum")+map.get("satisAnCostSum"));// 供应商+数据管理
            map.put("smoCostSum",map.get("smoCostSum"));// 供应商smo费用
            map.put("croCostSum",map.get("croCostSum"));// 供应商 CRo
            map.put("typeOtherSumCost","typeOtherSumCost");//其他总费用
            map.put("typeArticleSumCost","typeArticleSumCost");// 文章发表总费用
            // 里程碑时间
            map.put("firstGroupTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("lastOneGroupTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("subEndVisitTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("databaseLockTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("crsStudyReportTime",DateUtil.format(new Date(),"yyyy-MM"));


            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 防止中文乱码
            String fileName = URLEncoder.encode("项目费用信息", "UTF-8")
                    .replaceAll("\\+","%20");
            response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + fileName + ExcelTypeEnum.XLSX.getValue());
            //响应的输出流
            outputStream = response.getOutputStream();
            ClassPathResource classPathResource = new ClassPathResource("excel/projectCostConfig.xlsx");
            InputStream templateFileName = classPathResource.getInputStream();
            //String fileName = "D://" + "proCostConfig" + System.currentTimeMillis() + "demo.xlsx";

            excelWriter = EasyExcel.write(outputStream).withTemplate(templateFileName).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();

            excelWriter.fill(map, writeSheet);

            excelWriter.fill(new FillWrapper("year", yearResolves),fillConfig, writeSheet);
        }catch (Exception e){

        }finally {
            if(excelWriter!=null){
                excelWriter.finish();
                excelWriter.close();
            }
            if(outputStream!=null){
                outputStream.close();
            }
        }

    }


    /**
     * 测试新模版
     * @param response
     * @throws IOException
     */
    @GetMapping("test2")
    public void testDetail(HttpServletResponse response) throws IOException {

        ExcelWriter excelWriter=null;
        ServletOutputStream outputStream=null;

        TestExcelDemo testExcelDemo = new TestExcelDemo();
        List<YearResolve> yearResolves = testExcelDemo.initYear();

        try  {

            // 写入list之前的数据
            Map<String, Object> map = new ConcurrentHashMap<>();
            //研究中心总费用
            initDataStuCoreSumCost(map);
            //药品总费用
            initDataMedicineCostSum(map);
            //供应商总费用
            initDataSupSumCost(map);
            // 其他总费用
            initDataOtherSumCost(map);
            //文章发表总费用
           initDataArticle(map);
            //Ⅵ-赠药成本汇总
            //Ⅶ-人力成本总费用

            map.put("projectName", "项目名称");
            map.put("versionTime", "2019年10月9日13:28:28");
            map.put("projectCode", "方案版本号");
            map.put("createReportTime", DateUtil.format(new Date(),"yyyy-MM-dd"));
            map.put("coreNum", 4343);
            map.put("subNum", 434);
            map.put("proBegTime", "2023-07-21"); // 项目开始时间
            map.put("proEndTime", "2023-07-21");// 项目结束时间
            map.put("typeStuCoreSumCost", map.get("typeStuCoreSumCost")); //研究中心总费用
            map.put("typeMedicineSumCost", map.get("typeMedicineSumCost"));//药品总费用
            map.put("typeSupSumCost", map.get("typeSupSumCost"));//供应商总费用
            map.put("manySystemCostSumAdd", (String)map.get("manySysCostSum")+map.get("satisAnCostSum"));// 供应商+数据管理
            map.put("smoCostSum",map.get("smoCostSum"));// 供应商smo费用
            map.put("croCostSum",map.get("croCostSum"));// 供应商 CRo

            // 实际/对外总成本(中心总费用+药品总费用+供应商总费用+其他总费用+文章发表总费用)
            double actCostSum=0.0;
            double stuCoreSumCost = (double)map.get("typeStuCoreSumCost");
            double medicineSumCost = (double)map.get("typeMedicineSumCost");
            double supSumCost = (double)map.get("typeSupSumCost");
            double otherSumCost = (double)map.get("typeOtherSumCost");
            double otherCoreActBedSum = (double)map.get("typeArticleSumCost");
            BigDecimal decimal = NumberUtil.add(stuCoreSumCost, medicineSumCost, supSumCost, otherSumCost, otherCoreActBedSum);
            actCostSum= decimal.doubleValue();
            map.put("actCostSum",actCostSum);
            // 附加总成本(赠药费用和人力成本)
            double preManCostSum=0.0;
//            double mediSumCost =(double) map.get("typeFreeMediSumCost");
//            double manSumCost = (double)map.get("typeManSumCost");
            preManCostSum = NumberUtil.add(100.00, 200.00);
            map.put("preManCostSum",preManCostSum);
            // 项目总费用（实际成本/+附加总成本）
            double proCostSum=0.0;
            proCostSum= NumberUtil.add(actCostSum,preManCostSum);
            map.put("proCostSum",proCostSum);
            // 里程碑时间
            map.put("firstGroupTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("lastOneGroupTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("subEndVisitTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("databaseLockTime",DateUtil.format(new Date(),"yyyy-MM"));
            map.put("crsStudyReportTime",DateUtil.format(new Date(),"yyyy-MM"));


            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 防止中文乱码
            String fileName = URLEncoder.encode("项目费用信息", "UTF-8")
                    .replaceAll("\\+","%20");
            response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + fileName + ExcelTypeEnum.XLSX.getValue());
            //响应的输出流
            outputStream = response.getOutputStream();
            ClassPathResource classPathResource = new ClassPathResource("excel/proCostDetail.xlsx");
            InputStream templateFileName = classPathResource.getInputStream();
            //String fileName = "D://" + "proCostConfig" + System.currentTimeMillis() + "demo.xlsx";

            excelWriter = EasyExcel.write(outputStream).withTemplate(templateFileName).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();

            excelWriter.fill(map, writeSheet);

            excelWriter.fill(new FillWrapper("year", yearResolves),fillConfig, writeSheet);
        }catch (Exception e){

        }finally {
            if(excelWriter!=null){
                excelWriter.finish();
                excelWriter.close();
            }
            if(outputStream!=null){
                outputStream.close();
            }
        }

    }




    /**
     * 研究中心总费用
     * @param map
     * @return
     */
    public Map<String,Object> initDataStuCoreSumCost(Map<String,Object> map){
        // 研究者费
        map.put("stuUnitNum", 10); map.put("stuUnitCost", "100");map.put("stuCostSum", ""); map.put("stuRemark", "beizhu"); map.put("stuActBedSum", "");map.put("stuCoreActBedSum", "");
        //入组推荐费
        map.put("groupUnitNum", "groupUnitNum");map.put("groupUnitCost", "groupUnitCost");map.put("groupCostSum", "groupCostSum");map.put("groupRemark", "groupRemark");map.put("groupActBedSum", "groupActBedSum");map.put("groupCoreActBedSum", "groupCoreActBedSum");
        // 患者补贴
        map.put("subUnitNum", "subUnitNum");map.put("subUnitCost", "subUnitCost");map.put("subUnitCostSum", "subUnitCostSum");map.put("subRemark", "subRemark");map.put("subActBedSum", "subActBedSum");map.put("subCoreBedActSum", "subCoreBedActSum");
        // 研究中心管理费（管理费30%+税费6%）
        map.put("stuCoreManageUnitNum", "stuCoreManageUnitNum");map.put("stuCoreManageUnitCost", "stuCoreManageUnitCost");map.put("stuCoreManageCostSum", "stuCoreManageCostSum");map.put("stuCoreManageRemark", "stuCoreManageRemark");map.put("stuCoreManageActSum", "stuCoreManageActSum");map.put("stuCoreManageActBedSum", "stuCoreManageActBedSum");
        // 其他
        map.put("stuCoreOthUnitNum", "stuCoreOthUnitNum");map.put("stuCoreOthUnitCost", "stuCoreOthUnitCost");map.put("stuCoreOthCostSum", "stuCoreOthCostSum");map.put("stuCoreOthRemark", "stuCoreOthRemark");map.put("stuCoreOthActSum", "stuCoreOthActSum");map.put("stuCoreOthActBedSum", "stuCoreOthActBedSum");
        // 研究中心总费用
        map.put("typeStuCoreSumCost",0.0);

        return map;

    }

    /**
     *  药品总费用
     * @param map
     * @return
     */
    public Map<String,Object> initDataMedicineCostSum(Map<String,Object> map){
        // 药品外购
        map.put("drugOutUnitNum", "drugOutUnitNum");map.put("drugOutUnitCost", "drugOutUnitCost");map.put("drugOutUnitCostSum", "drugOutUnitCostSum");map.put("drugOutRemark", "drugOutRemark");map.put("drugOutActBedSum", "drugOutActBedSum");map.put("drugOutActBedSum", "drugOutActBedSum");
        // 药物处理(运输费)
        map.put("mediDealUnitNum", "mediDealUnitNum");map.put("medicineStoreUnitCost", "medicineStoreUnitCost");map.put("medicineStoreCostSum", "medicineStoreCostSum");map.put("medicineStoreRemark", "medicineStoreRemark");map.put("medicineStoreActBedSum", "medicineStoreActBedSum");map.put("mediDealCoreActBedSum", "mediDealCoreActBedSum");
        // 药品储藏
        map.put("medicineStoreUnitNum", "medicineStoreUnitNum");map.put("mediDealUnitCost", "mediDealUnitCost");map.put("mediDealCostSum", "mediDealCostSum");map.put("mediDealRemark", "mediDealRemark");map.put("mediDealActBedSum", "mediDealActBedSum");map.put("medicineStoreCoreActBedSum", "medicineStoreCoreActBedSum");
        // 药品销毁费
        map.put("mediDesUnitNum", "mediDesUnitNum");map.put("mediDesUnitCost", "mediDesUnitCost");map.put("mediDesCostSum", "mediDesCostSum");map.put("mediDesRemark", "mediDesRemark");map.put("mediDesActBedSum", "mediDesActBedSum");map.put("MediDesCoreActBedSum", "MediDesCoreActBedSum");
        //  其他
        map.put("mediOthUnitNum", "mediOthUnitNum");map.put("mediOthUnitCost", "mediOthUnitCost");map.put("mediothCostSum", "mediothCostSum");map.put("mediOthRemark", "mediOthRemark");map.put("mediOthActBedSum", "mediOthActBedSum");map.put("mediOthCoreActBedSum", "mediOthCoreActBedSum");
        // Ⅱ-药品总费用（不包括赠药）
        map.put("typeMedicineSumCost", 0.0);

        return map;
    }

    /**
     * 供应商总费用
     * @param map
     * @return
     */
    public Map<String,Object> initDataSupSumCost(Map<String,Object> map){
        // EDC系统+随机系统+数据管理
        map.put("manySysUnitNum", "manySysUnitNum");map.put("manySysUnitCost", "manySysUnitCost");map.put("manySysCostSum", "manySysCostSum");map.put("manySysRemark", "manySysRemark");map.put("manySysActBedSum", "manySysActBedSum");map.put("manySysCoreActBedSum", "manySysCoreActBedSum");
        // 统计分析外包费用
        map.put("satisAnUnitNum", "satisAnUnitNum");map.put("satisAnUnitCost", "satisAnUnitCost");map.put("satisAnCostSum", "satisAnCostSum");map.put("satisAnRemark", "satisAnRemark");map.put("satisAnActBedSum", "satisAnActBedSum");map.put("satisAnCoreActBedSum", "satisAnCoreActBedSum");
        // SMO费用
        map.put("smoUnitNum", "smoUnitNum");map.put("smoUnitCost", "smoUnitCost");map.put("smoCostSum", "smoCostSum");map.put("smoRemark", "smoRemark");map.put("smoActBedSum", "smoActBedSum");map.put("smoCoreActBedSum", "smoCoreActBedSum");
        //  CRO费用
        map.put("croUnitNum", "croUnitNum");map.put("croUnitCost", "croUnitCost");map.put("croCostSum", "croCostSum");map.put("croRemark", "croRemark");map.put("croRemark", "croRemark");map.put("croCoreActBedSum", "croCoreActBedSum");
        //  中心实验室费用（如适用）
        map.put("corelabUnitNum", "corelabUnitNum");map.put("corelabUnitCost", "corelabUnitCost");map.put("corelabCostSum", "corelabCostSum");map.put("corelabRemark", "corelabRemark");map.put("corelabActBedSum", "corelabActBedSum");map.put("corelabCoreActBedSum", "corelabCoreActBedSum");
        // 独立影像/病理评估费用（如适用）
        map.put("aloPathUnitNum", "aloPathUnitNum");map.put("aloPathUnitCost", "aloPathUnitCost");map.put("aloPathCostSum", "aloPathCostSum");map.put("aloPathRemark", "aloPathRemark");map.put("aloPathActBedSum", "aloPathActBedSum");map.put("aloPathCoreActBedSum", "aloPathCoreActBedSum");
        //  其他（如适用）
        map.put("supOtherUnitNum", "supOtherUnitNum");map.put("supOtherUnitCost", "supOtherUnitCost");map.put("supOtherCostSum", "supOtherCostSum");map.put("supOtherRemark", "supOtherRemark");map.put("supOtherActBedSum", "supOtherActBedSum");map.put("supOtherCoreActBedSum", "supOtherCoreActBedSum");
        // Ⅲ-供应商总费用
        map.put("typeSupSumCost", 0.0);

        return map;
    }

    /**
     * 其他总费用
     * @param map
     * @return
     */
    public Map<String,Object> initDataOtherSumCost(Map<String,Object> map){
        // 本地会议 (研究中心启动会)
        map.put("localCoreUnitNum", "localCoreUnitNum");map.put("localCoreUnitCost", "localCoreUnitCost");map.put("localCoreCostSum", "localCoreCostSum");map.put("localCoreRemark", "localCoreRemark");map.put("localCoreActBedSum", "localCoreActBedSum");map.put("localCoreActBedCostSum", "localCoreActBedCostSum");
        // 本地会议 (全体研究者会议)
        map.put("localAllUnitNum", "localAllUnitNum");map.put("localAllUnitCost", "localAllUnitCost");map.put("localAllCostSum", "localAllCostSum");map.put("localAllRemark", "localAllRemark");map.put("localAllActBedSum", "localAllActBedSum");map.put("localAllCoreActBedSum", "localAllCoreActBedSum");
        // 中心立项费
        map.put("coreProUnitNum", "coreProUnitNum");map.put("coreProUnitCost", "coreProUnitCost");map.put("coreProCostSum", "coreProCostSum");map.put("coreProRemark", "coreProRemark");map.put("coreProActBedSum", "coreProActBedSum");map.put("coreProCoreActBedSum", "coreProCoreActBedSum");
        // 伦理审查费 (初审)
        map.put("ethicFirstUnitNum", "ethicFirstUnitNum");map.put("ethicFirstUnitCost", "ethicFirstUnitCost");map.put("ethicFirstCostSum", "ethicFirstCostSum");map.put("ethicFirstRemark", "ethicFirstRemark");map.put("ethicFirstActBedSum", "ethicFirstActBedSum");map.put("ethicFirstCoreActBedSum", "ethicFirstCoreActBedSum");
        // 伦理审查修正案及年度审核
        map.put("ethicYearUnitNum", "ethicYearUnitNum");map.put("ethicYearUnitCost", "ethicYearUnitCost");map.put("ethicYearCostSum", "ethicYearCostSum");map.put("ethicYearRemark", "ethicYearRemark");map.put("ethicYearActBedSum", "ethicYearActBedSum");map.put("ethicYearCoreActBedSum", "ethicYearCoreActBedSum");
        // 设备、资料印刷、快递等办公费
        map.put("szkUnitNum", "szkUnitNum");map.put("szkUnitCost", "szkUnitCost");map.put("szkCostSum", "szkCostSum");map.put("szkRemark", "szkRemark");map.put("szkActBedSum", "szkActBedSum");map.put("szkCoreActBedSum", "szkCoreActBedSum");
        // 保险
        map.put("insureUnitNum", "insureUnitNum");map.put("insureUnitCost", "insureUnitCost");map.put("insureCostSum", "insureCostSum");map.put("insureRemark", "insureRemark");map.put("insureActBedSum", "insureActBedSum");map.put("insureCoreActBedSum", "insureCoreActBedSum");
        // 不良事件的管理费
        map.put("aeMangeUnitNum", "aeMangeUnitNum");map.put("aeMangeUnitCost", "aeMangeUnitCost");map.put("aeMangeCostSum", "aeMangeCostSum");map.put("aeMangeRemark", "aeMangeRemark");map.put("aeMangeActBedSum", "aeMangeActBedSum");map.put("aeMangeCoreActBedSum", "aeMangeCoreActBedSum");
        // 业务招待费（公关及应急等）
        map.put("busReceUnitNum", "busReceUnitNum");map.put("busReceUnitCost", "busReceUnitCost");map.put("busReceCostSum", "busReceCostSum");map.put("busReceRemark", "busReceRemark");map.put("busReceActBedSum", "busReceActBedSum");map.put("busReceCoreActBedSum", "busReceCoreActBedSum");
        // 其他
        map.put("otherUnitNum", "otherUnitNum");map.put("otherUnitCost", "otherUnitCost");map.put("otherCostSum", "otherCostSum");map.put("otherRemark", "otherRemark");map.put("otherActBedSum", "otherActBedSum");map.put("otherCoreActBedSum", "otherCoreActBedSum");
        //  Ⅳ-其他总费用（会议，立项伦理费，办公费，保险和AE管理费，招待费等）
        map.put("typeOtherSumCost",0.0);

        return map;

    }

    /**
     * Ⅴ-文章发表总费用
     * @param map
     * @return
     */
    public Map<String,Object> initDataArticle(Map<String,Object>  map){
        // 文章发表费用-会议poster
        map.put("artPosterUnitNum", "artPosterUnitNum");map.put("artPosterUnitCost", "artPosterUnitCost");map.put("artPosterCostSum", "artPosterCostSum");map.put("artPosterRemark", "artPosterRemark");map.put("artPosterActBedSum", "artPosterActBedSum");map.put("artPosterCoreActBedSum", "artPosterCoreActBedSum");
        // 文章发表费用-文章发表
        map.put("artPublicUnitNum", "artPublicUnitNum");map.put("artPublicUnitCost", "artPublicUnitCost");map.put("artPublicCostSum", "artPublicCostSum");map.put("artPublicRemark", "artPublicRemark");map.put("artPublicActBedSum", "artPublicActBedSum");map.put("artPublicCoreActBedSum", "artPublicCoreActBedSum");
        // 其他
        map.put("artOtherUnitNum", "artOtherUnitNum");map.put("artOtherUnitCost", "artOtherUnitCost");map.put("artOtherCostSum", "artOtherCostSum");map.put("artOtherRemark", "artOtherRemark");map.put("artOtherActBedSum", "artOtherActBedSum");map.put("artOtherCoreActBedSum", "artOtherCoreActBedSum");
        // Ⅴ-文章发表总费用
        map.put("typeArticleSumCost",0.0);
        return map;
    }




}
